<template>
  <div class="edit-container">
    <!-- 导航栏 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" />
    <!-- 头像 -->
    <div class="photo">
      <van-image
        width="120px"
        height="120px"
        fit="cover"
        round
        :src="userInfo.photo"
      >
        <van-uploader :after-read="afterRead" />
      </van-image>
    </div>
    <!-- ------------------------------单元格 -->
    <van-cell title="名称" :value="userInfo.name" is-list @click="editClick" />
    <van-cell
      title="性别"
      :value="userInfo.gender ? '女' : '男'"
      is-list
      @click="showPopup = true"
    />
    <van-cell
      title="生日"
      :value="userInfo.birthday"
      is-list
      @click="showDataTime = true"
    />

    <van-dialog
      v-model="dataShow"
      title="编辑用户页面"
      show-cancel-button
      @confirm="updateNickname"
    >
      <van-field v-model="newName" ref="input" placeholder="请输入用户名" />
    </van-dialog>
    <!-- 性别 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar title="性别" left-arrow @click-left="showPopup = false" />
      <van-cell title="男" is-link @click="updateGender(0)" />
      <van-cell title="女" is-link @click="updateGender(1)" />
    </van-popup>
    <!-- 生日 -->
    <van-popup v-model="showDataTime" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDataTime = false"
        @confirm="changedTime"
    /></van-popup>
    <!-- 图片裁切 -->
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropHeight="120"
        autoCropWidth="120"
      >
      </vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="upDataPhoto">确定</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo } from './user.js'
import { editUserPhoto } from './user.js'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  components: {
    VueCropper
  },
  data () {
    return {
      newName: '',
      dataShow: false,
      showPopup: false,
      minDate: new Date(1620, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      showDataTime: false,
      img: '',
      showCropper: false
    }
  },
  methods: {
    editClick () {
      // 监听弹出框得显示与隐藏
      this.dataShow = true
      // 监控弹出框里面的文本内容
      this.newName = this.userInfo.name
      // 获取输入框里面的焦点，定时
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    updateNickname () {
      editUserInfo({ name: this.newName }).then(res => {
        //(1)弹窗提示
        this.$toast.success('更新成功!')
        //(2)储存、更新vuex数据
        this.$store.commit('changeRes', {
          ...this.userInfo, //先传入原始数据
          name: this.newName //然后使用新的name覆盖原始的name
        })
      })
    },
    updateGender (gender) {
      editUserInfo({ gender }).then(res => {
        this.$toast.success('更新成功!')
        //(2)储存、更新vuex数据
        this.$store.commit('changeRes', {
          ...this.userInfo, //先传入原始数据
          gender //然后使用新的gender覆盖原始的gender
        })
        this.showPopup = false
      })
    },
    changedTime () {
      // 引入时间戳
      let birthday = moment(this.currentDate).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        this.$store.commit('changeRes', {
          ...this.userInfo,
          birthday
        })
        this.showDataTime = false
      })
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      // console.log(file)
      this.img = file.content
      this.showCropper = true
    },
    upDataPhoto () {
      this.$refs.cropper.getCropBlob(data => {
        console.log(data)
        // do something
        let fd = new FormData()
        fd.append('photo', data)
        this.showCropper = false
        editUserPhoto(fd).then(res => {
          console.log(res)
          this.$store.commit('changeRes', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
        })
      })
    }
  },
  // 把vuex里面的数据扩展出来
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less">
.edit-container {
  .van-nav-bar__content {
    background: rgb(25, 137, 250);
    .van-icon-arrow-left {
      color: #fff;
    }
  }

  .van-nav-bar__title {
    color: #fff;
  }
  .photo {
    display: flex;
    height: 150px;
    justify-content: center;
    align-items: center;
  }
  .van-uploader {
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0.5;
    width: 120px;
    height: 120px;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
  // 图片裁切
  .cropper {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
